﻿<%@ WebHandler Language='C#' Class='AnalysisFinaceType' %>

using System;
using System.Web;
using System.Data;
using System.Text;

using BLL;
using Library;

public class AnalysisFinaceType : IHttpHandler,System.Web.SessionState.IReadOnlySessionState 
 {
    private int MinYear = 2001;
    private int MaxYear = 9999;
    
    /// <summary>
    ///  财务类型
    /// </summary>
    private int TypeID
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["TypeID"] == null || HttpContext.Current.Request.QueryString["TypeID"] == String.Empty)
            {
                return FinaceTypeIDLink.PayOutID;
            }

            return int.Parse(HttpContext.Current.Request.QueryString["TypeID"]);
        }
    }


    private DateTime StartDate
    {
        get
        {
            string startDate = HttpContext.Current.Request.QueryString["StartDate"];
            if ( startDate == null || startDate == String.Empty)
            {
                return new DateTime(MinYear, 1, 1);
            }

            return DateTime.Parse(startDate);
        }
    }

    private DateTime EndDate
    {
        get
        {
            string endDate = HttpContext.Current.Request.QueryString["EndDate"];
            if (endDate == null || endDate == String.Empty)
            {
                return new DateTime(MaxYear , 1 , 1);
            }

            return DateTime.Parse(endDate);
        }
    }
    
    public void ProcessRequest (HttpContext context) {
        
         
        context.Response.ContentType = "text/xml";
        context.Response.Write(this.GetXml());

    
        context.Response.Expires = -1;
    }

    private string BuildTitle(DataTable dt)
    {
        FinaceTypeBll typeBll = new FinaceTypeBll();
        string title = typeBll.SelectSingleFinaceType(this.TypeID)
                       .TypeName;


        object obj = dt.Compute("sum(Amount)",String.Empty);

        decimal amount = 0;
        if (obj != DBNull.Value)
        {
            amount = (decimal)obj;
        }

        title = title + "(￥" + amount.ToString() + ")";
        string titleXml = "<vc:Chart.Titles><vc:Title Text='"+title+"' FontSize='14' /></vc:Chart.Titles> ";
        return titleXml;
    }

    private string BuildDataPoint(DataTable dt)
    {
       
        StringBuilder sb = new StringBuilder();
        foreach (DataRow dr in dt.Rows)
        {
            sb.AppendFormat("<vc:DataPoint AxisXLabel='{0}' YValue='{1}' {2} LabelFontFamily='宋体' />"
                        , dr["TypeName"].ToString()
                        , dr["amount"].ToString()
                        , GetLinkUrl(dr)
                      );
        }
        
//        string point =        @"
//                                <vc:DataPoint AxisXLabel='China'  YValue='32'/>
//                                <vc:DataPoint AxisXLabel='Russia'  YValue='27'/>
//                                <vc:DataPoint AxisXLabel='Australia' YValue='17'/>
//                                <vc:DataPoint AxisXLabel='Japan' YValue='16'/>
//                                <vc:DataPoint AxisXLabel='Japan' YValue='16'/>";
        
        return sb.ToString();
    }

    private string GetLinkUrl(DataRow dr)
    {
        string childsCount = dr["ChildsCount"].ToString();
        if ( childsCount == "0")
        {
            return String.Empty;
        }

        string url = " Href = 'AnalysisFinaceType.aspx?TypeID=" + dr["TypeID"].ToString();

        if (this.StartDate.Year != MinYear)
        {
            url += "&amp;StartDate=" + this.StartDate.ToYYYYMMDD();
        }

        if (this.EndDate.Year != MaxYear)
        {
            url += "&amp;EndDate=" + this.EndDate.ToYYYYMMDD();
        }

        url += "'";
        
        return  url;
    }
    
    
    private string GetXml()
    {
        string chart = @"<vc:Chart xmlns:vc='clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts' DataPointWidth='10'  Width='750' Height='300' BorderThickness='0.5' Padding='3' AnimationEnabled='true' Theme='Theme1' Watermark='False' >
                        @@Title        
                        <vc:Chart.Series>
                            <vc:DataSeries RenderAs='Column' LabelEnabled='True'>
                                <vc:DataSeries.DataPoints>
                                    @@DataPoint                                
                                </vc:DataSeries.DataPoints>                                
                            </vc:DataSeries>  
                        </vc:Chart.Series>
                        </vc:Chart>";


        AnalysisFinace analysis = new AnalysisFinace();

        DataTable dt = analysis.AnalyzeFinaceType(this.TypeID, HttpMemberInfo.MemerID, StartDate, EndDate);
        
         //<vc:Chart.AxesX>
         //               <vc:Axis AxisMaximum='9' />
         //               </vc:Chart.AxesX>
       chart = chart.Replace("@@Title",this.BuildTitle(dt));
       chart = chart.Replace("@@DataPoint", this.BuildDataPoint(dt));

       return chart;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}